package com.example.system01.controller;

import org.springframework.ui.Model;
import com.example.system01.entity.User;
import com.example.system01.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    HttpServletRequest request;
    //    处理前端的用户请求，或者给前端传递数据
    @RequestMapping("/")
    public String main(Model model){
        return "login";
    }
    @RequestMapping("/main")
    public String index(Model model){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
            model.addAttribute("username",username);
            return "main";
        }
    }
    @RequestMapping("/register")
    public String reg(){
        return "register";
    }
    //    处理一下用户的注册
    @RequestMapping("/registerProcess")
    public String register(@RequestParam String username, @RequestParam String password){
//        接收到前端发送过来的数据
        System.out.println("接收到了数据----"+username);
        if(username.isEmpty()){
            return "redirect:/error";
        }else {
//        将数据组装成user实体对象
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setRights("0");
            user.setStatus("0");

//        调用service的add添加方法将对象添加到数据库
            if (userService.add(user) == 1) {
                return "redirect:/";
            } else {
                return "redirect:/register";
            }
//        如果成功，就进入login登录页面，否则，就提示错误
        }
    }
    //  处理用户的登录
    @RequestMapping("/loginProcess")
    public String loginProcess(HttpServletRequest request, @RequestParam String username, @RequestParam String password){
//        确认接收到参数
        if(username.isEmpty()){
            System.out.println("登录失败");
            return "redirect:/error";
        }else{
//            1.组装一下user实体对象
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
//            2。调用service的findOne方法去查看数据库是否有前端发送的用户名和密码
            User userdb = userService.findOne(user);
            if (userdb.getUsername().isEmpty()){
                System.out.println("登录失败");
                return "redirect:/error";
            }
            else {
                if(userdb.getPassword().equals(password)){
//                    把当前用户存下来
                    HttpSession session = request.getSession();
                    session.setAttribute("username",username);
                    System.out.println("欢迎登录");
                    return "redirect:/main";
                }else{
                    System.out.println("登录失败");
                    return "redirect:/error";
                }
            }
        }
    }
    @RequestMapping("/logout")
    public String logout(){
        request.getSession().removeAttribute("username");
        return "redirect:/";
    }
}
